﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Easily._02_Form._01_frmCtrlConfig
{
    public partial class frmDigit : Form
    {
        private GeneralIcon MyIcon = null;  //保存传入的控件对象 
        string digitShow = "";//数码管显示的参数
        public frmDigit(GeneralIcon icon)
        {
            InitializeComponent();
            this.MyIcon = icon;
            //默认显示数字
            if(this.MyIcon.code.Save == "")
                ChangePanel();
            else
            {
                //保存的参数共有3项，以英文分号';'分开
                string[] tmp = this.MyIcon.code.Save.Split(';');
                if(tmp.Length>0)
                {
                    this.checkBox1.Checked = Boolean.Parse(tmp[0]);
                    if (this.checkBox1.Checked)
                    {
                        this.textBox1.Text = tmp[1];
                    }
                    else
                    {
                        if(tmp[1]!=null&&tmp[1]!="")
                        {
                            comboBox1.Items.Add(tmp[1]);
                            comboBox1.SelectedItem = tmp[1];
                        }
                    }
                }                
            }
            btn_confirm_Click(new object(), new EventArgs());//触发确认 根据保存的内容生成代码
        }

        /// <summary>
        /// 根据isNum也就是checkBox的选择 切换显示的面板
        /// </summary>
        public void ChangePanel()
        {
            if (this.checkBox1.Checked)
            {
                //显示的是数字 则让panel1显示
                this.panel1.Visible = true;
                this.panel1.Enabled = true;
                this.panel2.Visible = false;
                this.panel2.Enabled = false;
            }
            else
            {
                //相反 则让panel2显示
                this.panel2.Visible = true;
                this.panel2.Enabled = true;
                this.panel1.Visible = false;
                this.panel1.Enabled = false;
            }
        }

        /// <summary>
        /// 确认后生成数码管对应代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_confirm_Click(object sender, EventArgs e)
        {
            this.MyIcon.code.Clear();//清空以前代码
            //初始化数码管
            this.MyIcon.code.HeadFile = "#include \"" + this.MyIcon.info.head_file + "\""; //添加构件的头文件
            this.MyIcon.code.Initialization = "digitalTube_init(J8);//初始化数码管模块";
            //显示的是数字则获取textBox1中用户输入的数字
            if(this.checkBox1.Checked)
            {
                digitShow = this.textBox1.Text;
                this.MyIcon.code.InsertCode = "digitalTube_control(J8," + digitShow + ");"+
                    "//数码管显示" + digitShow;
            }
            //显示的不是数字
            if (!this.checkBox1.Checked && this.comboBox1.SelectedItem != null)
            {
                digitShow = this.comboBox1.SelectedItem.ToString();
                this.MyIcon.code.InsertCode = "digitalTube_control(J8,(float)" + digitShow + ");";
            }
            //Console.WriteLine(this.checkBox1.Checked.ToString());
            this.MyIcon.code.Save = this.checkBox1.Checked.ToString() + ";" + digitShow + ";";
            this.MyIcon.code.Comment = "数码管显示" + digitShow;

            //外部会对此进行判断，因此是必要的
            this.DialogResult = DialogResult.OK;
        }

        /// <summary>
        /// 默认显示数字 切换状态isNum取反
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            ChangePanel();
        }

        /// <summary>
        /// 让用户只能输入数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //如果输入的不是退格和数字，则屏蔽输入
            if (!(e.KeyChar == '\b' || (e.KeyChar >= '0' && e.KeyChar <= '9')))
            {
                e.Handled = true;
                MessageBox.Show("请输入数字！");
            }

        }

        /// <summary>
        /// 单击“取消”按钮
        /// 主要功能：设置返回结果为取消
        /// </summary>
        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        /// <summary>
        /// 点击“注释说明”按钮
        /// 功能概要：为控件添加自定义注释说明
        /// </summary>
        private void btn_comment_Click(object sender, EventArgs e)
        {
            using (frmNodeEditer NE = new frmNodeEditer(this.MyIcon.code.Comment))
            {
                if (NE.ShowDialog() == DialogResult.OK)
                    this.MyIcon.code.Comment = NE.NoteEdit;
            }
        }

        /// <summary>
        /// 打开窗体时 获取数码管能输出的数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void frmDigit_Load(object sender, EventArgs e)
        {
            ChangePanel();
            //清空comboBox重新载入item
            this.comboBox1.Items.Clear();
            //添加可供选择的系统变量到cBox_var中
            if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
            {
                //foreach (string value in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_Sensor_VariableLocal.Values)
                //{
                //    this.comboBox1.Items.Add(value);
                //}
                //添加系统变量
                if (Globaldefine.FrmParent.ActivedrmGraphicFlow != null)
                {
                    //全局变量
                    foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableSys)
                    {
                        this.comboBox1.Items.Add(item);
                    }
                    //传感器局部变量
                    foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_Sensor_VariableLocal.Values)
                    {
                        this.comboBox1.Items.Add(item);
                    }
                    //局部变量
                    foreach (var item in Globaldefine.FrmParent.ActivedrmGraphicFlow.m_VariableLocal)
                    {
                        comboBox1.Items.Add(item);
                    }
                }
            }
        }

        /// <summary>
        /// 当用户下拉时 如果items数量为0 说明用户没有配置终端 给予提示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            if(this.comboBox1.Items.Count==0)
            {
                MessageBox.Show("请先点击配置终端！或定义变量！");
            }
        }
    }
}
